package bookshopSM;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;


public class Logon extends JFrame{

    JButton jb1,jb2;
    JTextField jtf;
    JPasswordField jpwd;
    JLabel jl1,jl2;
    JRadioButton ja,jw;
    ButtonGroup radioGroup;
    JPanel jp1,jp2,jp3,jp4;
    static String stf;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";

    public static void main(String[] args) throws SQLException {

        // TODO Auto-generated method stub
        Logon logon=new Logon();
    }
    public Logon() throws SQLException
    {
        String path = "bgp.jpg";
        // 背景图片
        ImageIcon background = new ImageIcon(path);
        // 把背景图片显示在一个标签里面
        JLabel label = new JLabel(background);
        // 把标签的大小位置设置为图片刚好填充整个面板
        label.setBounds(0, 0, this.getWidth(), this.getHeight());
        // 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
        JPanel imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        // 把背景图片添加到分层窗格的最底层作为背景
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        //创建组件

    	/*Icon bug = new ImageIcon(getClass().getResource("bgp.jpg" ));
		JLabel label = new JLabel( bug, SwingConstants.CENTER );
		this.add(label,BorderLayout.CENTER);*/

        jb1=new JButton("登陆");
        jb2=new JButton("退出");

        jtf=new JTextField(10);
        jpwd=new JPasswordField(10);

        jl1=new JLabel("账    号：");
        jl2=new JLabel("密    码：");

        ja=new JRadioButton("管理员");
        jw=new JRadioButton("销售人员",true);

        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        jp4=new JPanel();
        //jp1.setBackground(Color.cyan);
        //设置布局管理器
        this.setLayout(new GridLayout(4,1));
        //添加组件
        jtf.setText("111111");
        jpwd.setText("123456");
        jp1.add(jl1);
        jp1.add(jtf);

        jp2.add(jl2);
        jp2.add(jpwd);

        jp3.add(ja);
        jp3.add(jw);
        radioGroup=new ButtonGroup();
        radioGroup.add(ja);
        radioGroup.add(jw);

        jp4.add(jb1);
        jp4.add(jb2);

        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);

        //设置窗体属性
        this.setTitle("欢迎登录图书销售管理系统");
        this.setSize(460, 280);
        // 获取屏幕尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        //获取主界面的窗体尺寸
        Dimension frameSize = this.getSize();
        this.setLocation((screenSize.width - frameSize.width) /2,
                (screenSize.height - frameSize.height) / 2);//居中显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        jb1.addActionListener(
                new ActionListener() {
                    String pwdget;
                    public void actionPerformed( ActionEvent event )
                    {
                        //JOptionPane.showMessageDialog(null, "添加成功");
                        if(ja.isSelected()){
                            try{

                                Class.forName(driverClassName); //或者Class.forName(driverClassName)...newInstance();
                                conn = DriverManager.getConnection(url,username,password);
                                Statement stmt=conn.createStatement();
                                ResultSet rs=stmt.executeQuery("select Acode from Admini "+
                                        " where Ano="+ jtf.getText());
                                while(rs.next()){
                                    pwdget = rs.getString("Acode");
                                }
                                char[] pwd = jpwd.getPassword();
                                String  pwdstr=new  String(pwd);
                                System.out.println("select Acode from Admini "+
                                        " where Ano="+ jtf.getText());
                                if(pwdstr.equals(pwdget)){
                                    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);//点击按钮时frame1销毁,new一个frame2
                                    stf=jtf.getText();
                                    new FrameAdmin();
                                }
                                else
                                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                            }
                            catch (Exception ee){
                                System.out.println(ee);
                            }
                            finally{
                                if(conn!=null){
                                    try {
                                        conn.close();
                                    } catch (SQLException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    conn=null;
                                }
                            }
                        }
                        else{
                            try{

                                //Class.forName(driverClassName); //或者Class.forName(driverClassName)...newInstance();
                                conn = DriverManager.getConnection(url,username,password);
                                Statement stmt=conn.createStatement();
                                ResultSet rs=stmt.executeQuery("select Wcode from Worker "+
                                        " where Wno="+ jtf.getText());
                                while(rs.next()){
                                    pwdget = rs.getString("Wcode");
                                }
                                char[] pwd = jpwd.getPassword();
                                String  pwdstr=new  String(pwd);
                                if(pwdstr.equals(pwdget)){
                                    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);//点击按钮时frame1销毁,new一个frame2
                                    stf=jtf.getText();
                                    new FrameWorker();
                                }
                                else
                                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                            }
                            catch (Exception ee){
                                System.out.println(ee);
                            }
                            finally{
                                if(conn!=null){
                                    try {
                                        conn.close();
                                    } catch (SQLException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    conn=null;
                                }
                            }

                        }
                    }
                } // end anonymous inner class
        );

        jb2.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        if(conn!=null){
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            conn=null;
                        }
                        System.exit(0);


                    }
                } // end anonymous inner class
        );

    }
    public static String getStf() {
        return stf;
    }
    public void setStf(String stf) {
        Logon.stf = stf;
    }
}
